import { useModel } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

const useAdministrator = () => {
    const { remoteData } = useModel("global");
    const { baseInfo } = useModel("person.info");
    const { selectedGateway } = useModel("gateway.info");

    const state = useReactive({
        loading: false,
        administrator: null,
    });

    const loadAdministratorInfo = async () => {
        if (!selectedGateway.url) return;
        state.loading = true;
        let res = await remoteData({
            object: "multi-person-gateway",
            p2pNodeUuid: selectedGateway.uuid,
            serviceUrl: selectedGateway.url,
            serviceName: "/administrator/getInfo",
            params: {
                uuid: baseInfo.uuid,
                gatewayUuid: selectedGateway.uuid,
            },
        });
        state.administrator = res || null;
        state.loading = false;
    };

    useEffect(() => {
        selectedGateway?.url && loadAdministratorInfo();
    }, [selectedGateway]);

    return { ...state, loadAdministratorInfo };
};

export default useAdministrator;
